home *** CD-ROM | disk | FTP | other *** search
- /*
- * Some simple routines to map the minimal standard
- * random number generator into ranges and distributions
- * of your choice.
- */
-
- /*
- * urand(): returns uniformly distributed
- * integers in the range lo..hi
- */
-
- int urand(int lo, int hi)
- {
- long tmp;
-
- tmp = stdrand();
- return((int)(tmp * (long)(hi - lo) + (long)lo));
- }
-
- /*
- * nrand(): returns normally distributed
- * integers in the range lo..hi
- */
-
- int nrand(int lo, int hi)
- {
- long tmp;
- int i;
-
- for(tmp=0, i=0; i<4; i++) {
- tmp += urand(lo,hi);
- }
- tmp = (tmp + 2L) / 4L;
-
- return((int) tmp);
- }
-
- /*
- * furand():
- * returns uniform random numbers in the
- * range 0.0 to 1.0 (exclusive)
- */
-
- float furand()
- {
- return(stdrand() / 2147483647.0);
- }
-
- /*
- * returns normal random numbers in the
- * range 0.0 to 1.0 (exclusive)
- */
-
- float fnrand()
- {
- float tmp;
- int i;
-
- for(tmp=0.0, i=0; i<4; i++) {
- tmp += furand();
- }
- return(tmp / 4.0);
- }
-